<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\BankSubAccountFinance;
use App\Models\MallDateFinance;
use App\Models\ProjectMall;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Http\Request;

class FinanceLogController extends Controller
{
    public function mtLogs(Request $request)
    {
        $dateLeft = $request->input("dateLeft","");
        $dateRight = $request->input("dateRight","");
        $page = $request->input("page",1);
        $pageSize = $request->input("pageSize",20);
        $projectId = $request->input("projectId","");
        $paginate = MallDateFinance::query()
            ->with([
                "mall"
            ])
            ->from("mall_date_finance as a")
            ->when($projectId, function (Builder $query)use($projectId) {
                $subQuery = ProjectMall::query()
                    ->from("project_mall as b")
                    ->join("project as c","b.project_id","=","c.id")
                    ->when($projectId, function (Builder $query)use($projectId) {
                        $query->where("b.project_id",$projectId);
                    })
                    ->groupBy("b.merchant_no")
                    ->select("b.merchant_no");
                $query->joinSub($subQuery, 'd', function (JoinClause $join) {
                    $join->on('d.merchant_no', '=', 'a.merchant_no');
                });
            })
            ->when($dateLeft, function (Builder $query)use($dateLeft) {
                $query->where("a.date",">=",$dateLeft);
            })
            ->when($dateRight, function (Builder $query)use($dateRight) {
                $query->where("a.date","<=",$dateRight);
            })
            ->where("settle_money",">",0)
            ->select("a.*")
            ->orderByDesc("a.date")
            ->paginate($pageSize,['*'],'page',$page);
        return $this->httpSuccess([
            "total"=>$paginate->total(),
            "items"=>$paginate->items(),
        ]);
    }

    public function bankLogs(Request $request)
    {
        $dateLeft = $request->input("dateLeft","");
        $dateRight = $request->input("dateRight","");
        $page = $request->input("page",1);
        $pageSize = $request->input("pageSize",20);
        $paginate = BankSubAccountFinance::query()
            ->when($dateLeft, function (Builder $query)use($dateLeft) {
                $query->where("create_time",">=",$dateLeft);
            })
            ->when($dateRight, function (Builder $query)use($dateRight) {
                $query->where("create_time","<=",$dateRight);
            })
            ->where("loan_type",2)
            ->select("bill_no","amount","create_time")
            ->orderByDesc("create_time")
            ->paginate($pageSize,['*'],'page',$page);
        return $this->httpSuccess([
            "total"=>$paginate->total(),
            "items"=>$paginate->items(),
            "totalMoney"=>BankSubAccountFinance::query()->where("loan_type",2)->sum("amount"),
            "balance"=>BankSubAccountFinance::query()->orderByDesc("create_time")->value("balance")
        ]);
    }
}
